package site.shihuan.helpdesk.service.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.HtmlUtil;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import site.shihuan.helpdesk.common.util.IdWorker;
import site.shihuan.helpdesk.service.dao.EsKnowledgeDao;
import site.shihuan.helpdesk.service.dao.SvcKnowledgeDao;
import site.shihuan.helpdesk.service.model.bo.KnowledgeSearchBO;
import site.shihuan.helpdesk.service.model.doc.EsKnowledge;
import site.shihuan.helpdesk.service.model.po.SvcKnowledgePO;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 知识库管理业务层
 *
 * @author 周世焕
 * @Date 2021/3/13 16:19
 */
@Component
@Slf4j
public class EsKnowledgeManager {

    @Autowired
    private EsKnowledgeDao esKnowledgeDao;

    @Autowired
    private SvcKnowledgeDao svcKnowledgeDao;

    /**
     * 查询知识库的答案
     * @param tenantId
     * @param question
     * @return
     */
    public List<EsKnowledge> findAnswer(String tenantId, String question){
        List<EsKnowledge> list = Lists.newArrayList(esKnowledgeDao.searchByQA(tenantId,question));
        try {
            if(list.get(0).getQuestion().equals(question)) {
                svcKnowledgeDao.increaseHit(tenantId,list.get(0).getId());
            }
        }catch (Exception e) {
            log.debug("更新查询次数错误，原因：",e.getMessage());
        }
        return list;
    }

    @Transactional(rollbackFor = Exception.class)
    public SvcKnowledgePO saveKnowledge(EsKnowledge esKnowledge) {
        /**
         * 数据库写
         */
        SvcKnowledgePO svcKnowledgePO = BeanUtil.toBean(esKnowledge,SvcKnowledgePO.class);
        String description = HtmlUtil.cleanHtmlTag(esKnowledge.getAnswer());

        if(description.length() > 80) {
            description = description.substring(0,80) + "...";
        }
        svcKnowledgePO.setDescription(description);
        svcKnowledgePO.setGmtModified(new Date(System.currentTimeMillis()));
        if(esKnowledge.getId()!=null) {
            svcKnowledgePO.setKnowledgeId(esKnowledge.getId());
            svcKnowledgeDao.updateByPrimaryKeySelective(svcKnowledgePO);
        }else {
            esKnowledge.setId(IdWorker.nextId());
            svcKnowledgePO.setKnowledgeId(esKnowledge.getId());
            esKnowledge.setCreated(System.currentTimeMillis());
            svcKnowledgeDao.insertSelective(svcKnowledgePO);
        }
        /**
         * Es写
         */
        esKnowledgeDao.save(esKnowledge);
        return svcKnowledgePO;
    }

    public List<SvcKnowledgePO> findHotKnowledgeList(String tenantId) {
        return svcKnowledgeDao.selectHotKnowledgeList(tenantId);
    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteKnowledge(String tenantId,String id) {
        /**
         * 数据库删
         */
        svcKnowledgeDao.deleteByTenantIdAndId(tenantId,id);
        /**
         * Es删
         */
        esKnowledgeDao.deleteById(id);
    }

    public List<SvcKnowledgePO> findKnowledgeByTenantId(KnowledgeSearchBO searchBO) {
        List<SvcKnowledgePO> knowledgePOS = svcKnowledgeDao.findByTenantId(searchBO);
        return knowledgePOS;
    }

    public SvcKnowledgePO findById(String id) {
        return svcKnowledgeDao.selectByPrimaryKey(id);
    }
}
